import Koa from 'koa'
import Router from 'koa-router'
import bodyParser from 'koa-bodyparser'
import cors from '@koa/cors'
import { Sequelize, DataTypes, Op } from 'sequelize'
// 实例化对象
let app = new Koa()
let router = new Router()
// 创建数据库实例
let sequelize = new Sequelize('test', 'root', 'Ye725601', {
    dialect: 'mysql',
    host: '8.152.214.221'
})
// 实例化数据库对象
let Book = sequelize.define('book', {
    name: {
        type: DataTypes.STRING
    },
    price: {
        type: DataTypes.FLOAT
    },
    author: {
        type: DataTypes.STRING
    }
})
// 连接数据库
// await sequelize.sync({ force: true })
// 定义端口
var port = 5000

app.use(bodyParser())
app.use(cors())

// 定义路由

/**
 * 查询所有图书
 * 查询单个图书
 * 新增图书
 * 修改图书
 * 删除图书
 */

router.get('/books', async (ctx, next) => {
    ctx.body = await Book.findAll()
})
router.get('/book/:id', async (ctx, next) => {
    ctx.body = '111'
})
router.post('/book', async (ctx, next) => {
    ctx.body = '111'

})
router.put('/book/:id', async (ctx, next) => {
    ctx.body = '111'

})
router.delete('/book/:id', async (ctx, next) => {
    ctx.body = '111'
})

// 注册路由
app.use(router.routes())

app.listen(port, () => {
    console.log(`Server is running : http://localhost:${port}`);
})
//